<?php
/**
 * Created by PhpStorm.
 * User: sogubaby
 * Date: 2019-03-04
 * Time: 15:20
 */

namespace Service;


use App\Jobs\CheckSpiderJob;
use App\Models\UserAuthSpiderTransLog;
use App\Models\UserLoanRequest;

trait Risk {
    /**
     * 申请风控
     * @param $loanRequestId
     * @throws \Exception
     * @author luwei
     * @date 2019-03-04 15:26
     */
    public function applyRisk($loanRequestId){
        $loan = UserLoanRequest::where('current',1)->byStatus(UserLoanRequest::STATUS_WAIT_FOR_RISK)->find($loanRequestId);
        if(!$loan){
            throw new \Exception('借款信息错误');
        }

        $tran = UserAuthSpiderTransLog::where('loan_request_id',$loanRequestId)
            ->groupBy('loan_request_id')
            ->having('status',UserAuthSpiderTransLog::STATUS_SUCCESS)
            ->get();
        if($tran){
            \Log::info('检测爬虫数据源:'.json_encode($tran,JSON_UNESCAPED_UNICODE));
            CheckSpiderJob::dispatch($tran);
        }else{
            throw new \Exception('缺少爬虫数据');
        }
    }
}